package apiserver

import (
	"gitee.com/youkelike/ziam/zoptions/options"
	"gitee.com/youkelike/zlog"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials"
)

type ExtraConfig struct {
	Addr         string
	MaxMsgSize   int
	ServerCert   options.GeneratableKeyCert
	mysqlOptions *options.MySQLOptions
	// etcdOptions      *genericoptions.EtcdOptions
}

func (c *ExtraConfig) Complete() *ExtraConfig {
	if c.Addr == "" {
		c.Addr = "127.0.0.1:8081"
	}
	return c
}

func (c *ExtraConfig) New() (*grpcAPIServer, error) {
	// 创建 grpc 服务
	creds, err := credentials.NewServerTLSFromFile(c.ServerCert.CertKey.CertFile, c.ServerCert.CertKey.KeyFile)
	if err != nil {
		zlog.Fatalw("Failed to generate credentials %s", err.Error())
	}
	opts := []grpc.ServerOption{grpc.MaxRecvMsgSize(c.MaxMsgSize), grpc.Creds(creds)}
	grpcServer := grpc.NewServer(opts...)

	// storeIns, _ := mysql.GetMysqlFactoryOr(c.mysqlOptions)

	// // grpc 的真实服务端
	// cacheIns := &cachev1.Cache{Store: storeIns}
	// pb.RegisterCacheServer(grpcServer, cacheIns)

	// reflection.Register(grpcServer)

	return &grpcAPIServer{grpcServer, c.Addr}, nil
}
